package org.easyx.ui.layui;

import java.util.ArrayList;
import java.util.List;

import org.easyx.core.entity.Constants;
import org.easyx.ser.sys.entity.SysResource;

public class LayuiKit {
	
	
	public static List<MenuEntity> genLayuiMenuJsonBySysResource(List<SysResource> list){
		if(list == null) return null;
    	List<MenuEntity> resultList = new ArrayList<MenuEntity>();
    	buildMenuTree(list,resultList,null,true);
		return resultList;
	}

	private static List<MenuEntity> buildMenuTree(List<SysResource> resList,
			List<MenuEntity> resultList, Long pid, boolean hasBtn) {
		if(resList == null) return null;
    	for(SysResource res : resList){
    		if(res != null 
    				&& (pid==null ? res.getPid()==null : res.getPid()!=null && res.getPid().compareTo(pid)==0) //pid=上级id
    				&& (hasBtn?true:res.getResourceType()==Constants.SysResource_MENU)){
    			
    			MenuEntity me = new MenuEntity();
    			me.setTitle(res.getName());
    			me.setIcon(res.getIcon());
    			me.setHref(res.getUrl().startsWith("/")?res.getUrl().substring(1, res.getUrl().length()):res.getUrl());
    			
    			//递归调用
    			List<MenuEntity> subList = new ArrayList<MenuEntity>();
    			buildMenuTree(resList,subList,res.getId(),hasBtn);
    			
    			me.setChildren(subList);
    			resultList.add(me);
    		}
    	}
    	return resultList;
	}
}
